﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cn.Edwon.Web.Support;
using Cn.Edwon.Biz;
using Cn.Edwon.Spring;
using Cn.Cellula.Module.Stat.Model;

namespace Cn.Cellula.Website.Admin.Module.Stat {
    public partial class EditParam : PageSupport {
        IBiz biz = BizFactory.Get<IBiz>();
        String EOKey = "";
        protected void Page_Load(object sender, EventArgs e) {
            EOKey = Request.QueryString["id"];
            if (!Page.IsPostBack) {
                InitForm();
                if (!String.IsNullOrEmpty(EOKey)) {
                    FillForm();
                } else {
                    String StatKey = Request.QueryString["sid"];
                    try {
                        InitParentEOInfo(StatKey);
                    } catch (Exception ex) {
                        log.Error(ex.Message);
                        this.lab_StatName.Text = "统计对象参数错误";
                        this.btn_save.Enabled = false;
                    }
                }
            }
        }

        private void InitForm() {
            this.ddl_FilterType.DataTextField = "ParamTypeName";
            this.ddl_FilterType.DataValueField = "ParamTypeKey";
            this.ddl_FilterType.DataSource = biz.LoadAll<ModTStatParamTypeEO>();
            this.ddl_FilterType.DataBind();
        }

        private void InitParentEOInfo(String rKey) {
            ModTStatEO eo = biz.Load<ModTStatEO>(rKey);
            this.lab_StatName.Text = eo.StatName;
            this.hid_StatKey.Value = eo.StatKey;
        }

        private void FillForm() {
            ModVStatParamEO eo = biz.Load<ModVStatParamEO>(EOKey);
            this.txt_DataField.Text = eo.ParamName;
            this.txt_DefaultValue.Text = eo.DefaultValue;
            this.txt_FilterLabel.Text = eo.ParamLabel;
            this.txt_FilterName.Text = eo.ParamTitle;
            this.txt_FormFieldKey.Text = eo.FormFieldKey;
            this.ddl_FilterType.SelectedValue = eo.ParamType+"";
            this.lab_StatName.Text = eo.StatName;
            this.hid_StatKey.Value = eo.StatKey;
            this.radl_DataType.SelectedValue = eo.DataType;

            this.hid_ItemCategoryKey.Value = eo.ItemCategoryKey;
            this.txt_ItemCategoryKey.Text = eo.ItemCategoryName;

            this.txt_OrderNumber.Text = eo.OrderNumber + "";
        }

        protected void btn_save_Click(object sender, EventArgs e) {
            if (String.IsNullOrEmpty(EOKey)) {
                ModTStatParamEO eo = new ModTStatParamEO();
                eo.StatKey = this.hid_StatKey.Value;
                eo.ParamName = this.txt_DataField.Text;
                eo.DefaultValue = this.txt_DefaultValue.Text;
                eo.ParamLabel = this.txt_FilterLabel.Text;
                eo.ParamTitle = this.txt_FilterName.Text;
                eo.ParamType = Convert.ToInt32(this.ddl_FilterType.SelectedValue);
                eo.FormFieldKey = this.txt_FormFieldKey.Text;
                eo.DataType = this.radl_DataType.SelectedValue;
                eo.ItemCategoryKey = this.hid_ItemCategoryKey.Value;
                try {
                    eo.OrderNumber = Convert.ToInt32(this.txt_OrderNumber.Text);
                } catch { }
                biz.Save<ModTStatParamEO>(eo);
            } else { //update
                ModTStatParamEO eo = biz.Load<ModTStatParamEO>(EOKey);
                eo.StatKey = this.hid_StatKey.Value;
                eo.ParamName = this.txt_DataField.Text;
                eo.DefaultValue = this.txt_DefaultValue.Text;
                eo.ParamLabel = this.txt_FilterLabel.Text;
                eo.ParamTitle = this.txt_FilterName.Text;
                eo.ParamType = Convert.ToInt32(this.ddl_FilterType.SelectedValue);
                eo.FormFieldKey = this.txt_FormFieldKey.Text;
                eo.DataType = this.radl_DataType.SelectedValue;
                eo.ItemCategoryKey = this.hid_ItemCategoryKey.Value;
                try {
                    eo.OrderNumber = Convert.ToInt32(this.txt_OrderNumber.Text);
                } catch { }
                biz.Update<ModTStatParamEO>(eo);
            }
            Response.Redirect("List.aspx?sid="+this.hid_StatKey.Value);
        }
    }
}